var db = require( '../db/db' );
var util = require( 'util' );

const tableName = 'core.user';

exports.getUser = async function ( id ) {
    var sql = `SELECT * FROM ${ tableName } WHERE id = $1`;
    var params = [ id ];

    try {

        var result = await db.runSql( sql, params );

        if ( result ) {
            return result.rows;
        }
        return null;

    } catch ( e ) {
        console.error( "查询用户出错" );
        return null;
    }
};


exports.getUserByEmail = async function ( email ) {
    var sql = `SELECT * FROM ${ tableName } WHERE email = $1`;
    var params = [ email ];

    try {

        var result = await db.runSql( sql, params );

        if ( result ) {
            return result.rows;
        }
        return null;

    } catch ( e ) {
        console.error( "查询用户出错" );
        return null;
    }

};

exports.updateEmail = async function ( userId, email ) {
    var sql = `UPDATE ${ tableName } SET email=$2, updated_at=now() WHERE id=$1`;
    var params = [ userId, email ];

    try {

        var result = await db.runSql( sql, params );

        if ( result ) {
            return result.rows;
        }
        return null;

    } catch ( e ) {
        console.error( "更新用户邮箱出错" );
        return null;
    }
}

exports.updateNickname = async function ( userId, nickname ) {
    var sql = `UPDATE ${ tableName } SET name=$2, updated_at=now() WHERE id=$1`;
    var params = [ userId, nickname ];

    try {

        var result = await db.runSql( sql, params );

        if ( result ) {
            return result.rows;
        }
        return null;

    } catch ( e ) {
        console.error( "更新用户昵称出错" );
        return null;
    }
}

exports.updatePassword = async function ( userId, hashPassword, salt ) {
    var sql = `UPDATE ${ tableName } SET hash_password=$2, salt=$3, updated_at=now() WHERE id=$1`;
    var params = [ userId, hashPassword, salt ];

    try {

        var result = await db.runSql( sql, params );

        if ( result ) {
            return result.rows;
        }
        return null;

    } catch ( e ) {
        console.error( "更新用户密码出错" );
        return null;
    }
}

exports.updatePassword = async function ( userId, hashPassword, salt ) {
    var sql = `UPDATE ${ tableName } SET hash_password=$2, salt=$3, updated_at=now() WHERE id=$1`;
    var params = [ userId, hashPassword, salt ];

    try {

        var result = await db.runSql( sql, params );

        if ( result ) {
            return result.rows;
        }
        return null;

    } catch ( e ) {
        console.error( "更新用户密码出错" );
        return null;
    }
}

exports.updateConfirmed = async function ( userId, confirmed ) {
    var sql = `UPDATE ${ tableName } SET confirmed=$2, updated_at=now() WHERE id=$1`;
    var params = [ userId, confirmed ];

    try {

        var result = await db.runSql( sql, params );

        if ( result ) {
            return result.rows;
        }
        return null;

    } catch ( e ) {
        console.error( "更新用户邮箱确认状态出错", e );
        return null;
    }
}